package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.dto.SetmealDTO;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.SetmealMapper;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.CheckItemService;
import com.itheima.health.service.SetmealService;
import com.itheima.health.utils.aliyunoss.AliyunUtils;
import com.itheima.health.utils.redis.RedisUtil;
import com.itheima.health.utils.resources.RedisConstant;
import com.itheima.health.vo.CheckGroupVO;
import com.itheima.health.vo.SetmealVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

// 2.7.8
@Service
@Transactional
public class StemealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private CheckItemService checkItemService;
    @Autowired
    private CheckGroupService checkGroupService;

    @Override
    public boolean saveSetmeal(SetmealDTO setmealDTO) {
        if (RedisUtil.existsKey(RedisConstant.SINGLE_PIC+setmealDTO.getImg())==false) {
        throw  new RuntimeException("超时,请重新提交图片");
        }
//        套餐录入 中间表数据 循环录入
        boolean save =save(setmealDTO);
        Integer sid = setmealDTO.getId();
        Integer[] checkgroupIds = setmealDTO.getCheckgroupIds();
        if (checkgroupIds!=null){
            for (Integer checkgroupId : checkgroupIds) {
                baseMapper.addSetmealAndGroupInfo(sid,checkgroupId);
            }
        }
//        set集合移除指定图片
        RedisUtil.removeSetMember(RedisConstant.ALL_SETMEAL_PIC_SET,setmealDTO.getImg());
        return save;
    }
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Page pageResult = null;
        //判断是否为空
        if (StringUtils.isNotBlank(queryPageBean.getQueryString())) {
            QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<Setmeal>();
            queryWrapper
                    .like("name",queryPageBean.getQueryString()).
                    or()
                    .like("HELPCODE", queryPageBean.getQueryString())
                    .or()
                    .like("code",queryPageBean.getQueryString());
            pageResult = page(new Page<Setmeal>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),queryWrapper);
        } else {
//            无条件的任务查询 业务层实现
            pageResult = page(new Page<Setmeal>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()));
        }
        return new PageResult(pageResult.getTotal(), pageResult.getRecords());
    }

    @Override
    public void clearImg() {
//        清理set集合里面的垃圾图片
        Set<String>  members = RedisUtil.getMembersOfSet(RedisConstant.ALL_SETMEAL_PIC_SET);
        for (String uuidfilename : members) {
            String exitFilenme= RedisConstant.SINGLE_PIC+uuidfilename;
            if (RedisUtil.existsKey(exitFilenme)==false){
//                true可以存在 没有过期 flase 过期 oss删除
                AliyunUtils.deleteFile(uuidfilename);
                RedisUtil.removeSetMember(RedisConstant.ALL_SETMEAL_PIC_SET,uuidfilename);
                System.out.println("垃圾图片清理了");
            }
        }
    }
//内连接查询 外连接 join on 关键字
    @Override
    public SetmealVO findSetMealDetail(int id) {
//        多表联查 完成五表数据 setmealVo对象
        SetmealVO setmealVO = baseMapper.findSetmealById(id);

      List<CheckGroupVO> checkGroupVOList= checkGroupService.findCheckGroupInfoBySetmealId(id);

        for (CheckGroupVO checkGroupVO : checkGroupVOList) {
        List<CheckItem>checkItemList=   checkItemService.findCheckItemByGroupId(checkGroupVO.getId());
        checkGroupVO.setCheckItems(checkItemList);
        }
        setmealVO.setCheckGroupVOList(checkGroupVOList);
        return setmealVO;
    }

    @Override
    public Setmeal findSetmealById(int id) {
        return baseMapper.selectById(id);
    }


}
